﻿using System;
using System.Web.UI.WebControls;
using System.Collections;
using WebPattern.Core.Logger;

/// <summary>
/// Summary description for BaseControl
/// </summary>
public abstract class BaseControl : System.Web.UI.UserControl
{
    #region Properties
    static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(typeof(System.Web.UI.UserControl));
    static readonly CustomLog4net _customLogger = new CustomLog4net(_logger);
    /// <summary>
    /// Loger
    /// </summary>
    public log4net.ILog Logger
    {
        get { return _logger; }
    }
    /// <summary>
    /// Custom Logger
    /// </summary>
    public static CustomLog4net CLogger
    {
        get { return _customLogger; }
    }
    /// <summary>
    /// Culture name 
    /// </summary>
    public string CurrentCultureName
    {
        get { if (Session["__currentLanguage"] != null) return Session["__currentLanguage"].ToString(); else return string.Empty; }
    }
    #endregion

    #region Copnstructor
    public BaseControl()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    #endregion

    #region Abstract Methods
    public abstract void InitControlsLanguage();
    #endregion

    #region Helper Methods

    #region Initialize DDL
    /// <summary>
    /// Load data from enumerator to dropDownList
    /// </summary>
    /// <param name="ddl">DropDownList</param>
    /// <param name="enumerator">Enumerator</param>
    public void InitDropDownList(DropDownList dropDownList, Type enumerator)
    {
        Hashtable ht = GetEnumForBind(enumerator);
        dropDownList.DataSource = ht;
        dropDownList.DataTextField = "value";
        dropDownList.DataValueField = "key";

        dropDownList.DataBind();
    }
    private Hashtable GetEnumForBind(Type enumeration)
    {
        string[] names = Enum.GetNames(enumeration);
        Array values = Enum.GetValues(enumeration);
        Hashtable ht = new Hashtable();

        for (int i = 0; i < names.Length; i++)
        {
            // Get the global resource string.
            try
            {
                // Look in the global resource file called MyResource.resx.
                string resKey = enumeration.Name + "_" + names[i];

                if ((String)GetGlobalResourceObject("enums", resKey) == null) throw new Exception("Could not find global resource.");
                ht.Add(Convert.ToInt32(values.GetValue(i)).ToString(), (String)GetGlobalResourceObject("enums", resKey));
            }
            catch
            {
                ht.Add(Convert.ToInt32(values.GetValue(i)).ToString(), names[i]);
            }
        }
        return ht;
    }
    #endregion

    #region Resources
    protected string GetLocalResource(string resourceKey)
    {
        try
        {
            return GetLocalResourceObject(resourceKey).ToString();
        }
        catch (Exception ex)
        {
            return string.Format("Value for resource key: '{0}' not found", resourceKey, ex.Message);
        }
    }
    #endregion

    #region ...
    
    #endregion

    #endregion
}